package com.ziyia.broacastbasepractivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class LoginActivity extends BaseActivity {
    private EditText account, password;
    private CheckBox checkBox;
    private Button login;
    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        account = findViewById(R.id.accountEdit);
        password = findViewById(R.id.passwordEdit);
        checkBox = findViewById(R.id.rememberPass);
        login = findViewById(R.id.login);


        SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
        boolean remember_password1 = prefs.getBoolean("remember_password", false);

        if (remember_password1) {
            account.setText(prefs.getString("account", ""));
            password.setText(prefs.getString("password", ""));
            checkBox.setChecked(true);
        }

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String a = account.getText().toString();
                String p = password.getText().toString();
                if ("admin".equals(a) && "123456".equals(p)) {

                    SharedPreferences.Editor edit = prefs.edit();
                    if (checkBox.isChecked()) {
                        edit.putBoolean("remember_password", true);
                        edit.putString("account", a);
                        edit.putString("password", p);
                    } else {
                        edit.clear();
                    }
                    edit.apply();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(LoginActivity.this, "帐号或密码不正确", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}
